﻿/**
 * ...
 * @author Lobanov A.V.
 */

package model
{
	import model.utils.DataXmlLoader;
	import org.puremvc.as3.interfaces.IProxy;
	import org.puremvc.as3.patterns.proxy.Proxy;
	import view.components.utils.UIEvent;
	
	public class DataProxy extends Proxy implements IProxy 
	{
		
		public static const NAME:String = "DataProxy";
		
		private var _cacheAllDate:Object;
		
		public function DataProxy(data:Object) 
		{
			super(NAME, new Object());
			
			_cacheAllDate = {};
		}
		
		/**
		 * Load target XML
		 * 
		 * For multiloading xml data this class 
		 * retrive XML data in function choiceModul.
		 * 
		 * @param url : String 
		 */
		public function loadXmlData(url:String) : void {
			new DataXmlLoader(url).addEventListener(DataXmlLoader.XML_LOADING_SUCCESS, handleXmlLoadingSuccess);
		}
		
		private function handleXmlLoadingSuccess(event : UIEvent) : void 
		{
			var dataXml:XML = event.data as XML;
			trace("=",_cacheAllDate[dataXml.@mod]);
			choiseModule(dataXml);
		}
		
		private function choiseModule($dataXml:XML):void {			
			switch ($dataXml.@mod) {
				case "core":
					sendNotification(ApplicationFacade.CORE_DATA_IS_LOAD, $dataXml);
				break;
				
				case "news":
					sendNotification(ApplicationFacade.NEWS_DATA_IS_LOAD, $dataXml);
				break;
				
				default: 
				break;
			}
		}
	}
}